package com.example.pm.ending.server.service.impl;

import com.example.pm.ending.server.ex.ServiceException;
import com.example.pm.ending.server.mapper.BuildUnitRelMapper;
import com.example.pm.ending.server.mapper.HouseBuildMapper;
import com.example.pm.ending.server.mapper.HouseUnitMapper;
import com.example.pm.ending.server.module.admin.AdminListEntityVO;
import com.example.pm.ending.server.module.property.house.AddNewBuildDTO;
import com.example.pm.ending.server.module.property.house.AddNewUnitDTO;
import com.example.pm.ending.server.module.property.house.BuildListVO;
import com.example.pm.ending.server.module.property.house.UpdateBuildDTO;
import com.example.pm.ending.server.pojo.entity.HouseBuild;
import com.example.pm.ending.server.pojo.entity.HouseUnit;
import com.example.pm.ending.server.pojo.vo.HouseBuildListVO;
import com.example.pm.ending.server.pojo.vo.HouseBuildStandardVO;
import com.example.pm.ending.server.pojo.vo.HouseUnitListVO;
import com.example.pm.ending.server.pojo.vo.HouseUnitStandardVO;
import com.example.pm.ending.server.service.IHouseModuleService;
import com.example.pm.ending.server.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

@Service
@Slf4j
public class HouseModuleServiceImpl implements IHouseModuleService {

    @Autowired
    HouseBuildMapper houseBuildMapper;
    @Autowired
    HouseUnitMapper houseUnitMapper;
    @Autowired
    BuildUnitRelMapper buildUnitRelMapper;

    /**
     * 区(栋)的业务
     * */
    @Override
    public void addNewBuild(AddNewBuildDTO addNewBuildDTO) {

        log.debug("开始处理[添加新区(栋)]的业务,传来参数为{}", addNewBuildDTO);
        //获取传入栋的名称
        String buildName = addNewBuildDTO.getName();
        //查询传入数据名称数量
        int countBuildName = houseBuildMapper.countByName(buildName);
        log.debug("传入栋(区)的名称{}",countBuildName);
        if(countBuildName>0){
            //名称已被占用
            String message = "添加栋(区)失败,名称已被占用!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }

        HouseBuild houseBuild = new HouseBuild();
        BeanUtils.copyProperties(addNewBuildDTO,houseBuild);
        log.debug("即将插入数据,参数:{}",houseBuild);
        houseBuildMapper.insert(houseBuild);



    }

    @Override
    public void deleteBuild(Long id) {
        log.debug("开始处理[删除区(栋)的业务,参数{}",id);
        HouseBuildStandardVO build = houseBuildMapper.getHouseBuildById(id);
        log.debug("根据id{}查询区(栋)是否存在,查询结果{}",id,build);
        if(build == null){
            //判断为null抛出异常
            String message = "删除该区(栋)失败,查询不到该区(栋)的存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        //成功,可以执行
        log.debug("即将执行删除操作,参数{}",id);
        houseBuildMapper.deleteById(id);
    }

    @Override
    public void updateBuild(Long id, UpdateBuildDTO updateBuildDTO) {
        log.debug("开始处理[区(栋)的修改]的业务，ID：{}，新数据：{}", id, updateBuildDTO);
        HouseBuildStandardVO queryResult = houseBuildMapper.getHouseBuildById(id);
        // 判断查询结果是否为null
        if (queryResult == null) {
            // 是：抛出异常
            String message = "修改相册详情失败，尝试修改的相册数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        //判断名称是否被占用
        int count = houseBuildMapper.countByName(updateBuildDTO.getName());
        if (count > 0) {
            // 是：名称被占用，抛出异常
            String message = "修改区(栋)数据失败，区(栋)名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        HouseBuild houseBuild = new HouseBuild();
        BeanUtils.copyProperties(updateBuildDTO, houseBuild);
        houseBuild.setId(id);
        houseBuildMapper.update(houseBuild);
    }

    @Override
    public List<HouseBuildListVO> buildList() {
        log.debug("开始处理[查询区(栋)]的业务");
        List<HouseBuildListVO> Buildist = houseBuildMapper.list();
        Iterator<HouseBuildListVO> iterator = Buildist.iterator();
        while (iterator.hasNext()) {

        }
        return Buildist;
    }



    /**
     * 单元的业务
     * */
    @Override
    public void addNewUnit(AddNewUnitDTO addNewUnitDTO) {
        log.debug("开始处理添加[新单元]的业务{}",addNewUnitDTO);
        //获取传入单元的名称
        String unitName = addNewUnitDTO.getName();
        int countUnitName = houseBuildMapper.countByName(unitName);
        if(countUnitName>0){
            String message = "添加单元失败,名称已被占用";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        HouseUnit houseUnit = new HouseUnit();
        BeanUtils.copyProperties(addNewUnitDTO,houseUnit);
        log.debug("即将插入数据,参数:{}",houseUnit);
        houseUnitMapper.insert(houseUnit);
    }

    @Override
    public void deleteUnit(Long id) {
        log.debug("开始处理[删除单元]业务,参数{}",id);
        int row = houseUnitMapper.countById(id);
        if (row == 0) {
            String message= "删除单元失败,想要删除的数据";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        houseBuildMapper.deleteById(id);
        log.debug("执行删除的{}的数据删除完成",id);



    }

    @Override
    public void updateUnit(Long id, UpdateBuildDTO updateBuildDTO) {
        log.debug("开始处理[修改单元]的业务,参数为{}",id);
        int row = houseUnitMapper.countById(id);
        if(row == 0){
            String message = "修改单元失败,没有查询到该单元的数据";
            log.warn(message);

        }
        int count = houseBuildMapper.countByName(updateBuildDTO.getName());
        if(count>0){
            String message= "修改单元失败,名称已被占用";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        HouseUnit houseUnit = new HouseUnit();
        BeanUtils.copyProperties(updateBuildDTO,houseUnit);
        houseUnit.setId(id);
        log.debug("修改{}单元的业务完成",id);
    }

    @Override
    public List<HouseUnit> unitList() {
        log.debug("开始执行查询[单元]的业务");
        List<HouseUnit> unitList = houseUnitMapper.list();
        Iterator<HouseUnit> iterator = unitList.listIterator();
        while (iterator.hasNext()) {

        }
        return unitList;
    }


}
